<%@page import="net.groupme.search.GroupMeSearchEngine"%>
<%@page import="net.groupme.user.UserManager"%>
<%@page import="java.util.List"%>
<%@page import="net.groupme.model.Resource"%>
<%@page import="net.groupme.model.*"%>
<%@page import="java.util.Iterator"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="net.groupme.gui.constants.VIEW_VOCABULARY"%>
<%@page import="net.groupme.model.ObjectFactory"%>
<%@page import="net.groupme.model.MODEL_CONSTANTS"%>
<div class="grouplistHead" style="margin-top: 1em; margin-bottom: 1em;">
	<div class="groupListHeadMenu">
	</div>
	<% String searchString = request.getParameter(VIEW_VOCABULARY.PARAMETER_KEYWORD); %>
	<h4>Your groups and resources associated with <i>"<%= searchString %>"</i><br/>
	<span style="font-size: x-small; font-weight: 100">Explore other groups/resources that are related to <i><%= searchString %></i>: 
	<% 	String tmpKeywordDelimeter = "";	
		if(searchString.contains(" ")){
			tmpKeywordDelimeter = "%22";
		}else{
			tmpKeywordDelimeter = "";
		}
	%>
	<a href="<%= request.getContextPath() +"/" + VIEW_VOCABULARY.ACTION_SEARCH + "?" + VIEW_VOCABULARY.PARAMETER_KEYWORD + "=" + tmpKeywordDelimeter + searchString + tmpKeywordDelimeter %>" title="Explore Groups and Resources related to <%= searchString %>">search</a>
	</span>
	</h4>
</div>

<% GroupMeSearchEngine se = (GroupMeSearchEngine) WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext())
						.getBean(VIEW_VOCABULARY.GROUPME_SEARCH_ENGINE, GroupMeSearchEngine .class); %>
<% UserManager um = (UserManager) WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext())
						.getBean(VIEW_VOCABULARY.USER_MANGER, UserManager.class); %>
<div id="mainGroupwindowContent">
		<% 
		List<Resource> myresources = se.getUserSpecificSearchStrategy().searchForOwnAndSubscribedResources(um.getUser(), ObjectFactory.getTag(request.getParameter(VIEW_VOCABULARY.PARAMETER_KEYWORD)));
		
		
		Iterator it = myresources.iterator();
		while (it.hasNext()){ %>
		<div class="groupWithinGroupList">
		<%
			Resource actualResource = (Resource) it.next(); 
			if (MODEL_CONSTANTS.RESOURCE_TYPE_GROUP.equals(actualResource.getType())){
				
				String thumbnail = null;
				String defaultgroupThumbnail = request.getContextPath() + "/images/groupimg-default.gif";
				String groupImageError = request.getContextPath() + "/images/groupErrorImg.png";
				String groupID = actualResource.getId();
			
				Group actualItem = ObjectFactory.getGroup(groupID);
				Iterator<User> userIter = actualItem.getUsers("owner").iterator();
				User groupOwner = null;
				if (userIter.hasNext()){
					groupOwner = userIter.next();
				}
    			thumbnail = actualItem.getActualGroupVisualization(groupOwner.getId()).getThumbnail();
    			if (thumbnail== null){
					thumbnail = actualItem.getURLOfAPictureInGroup();
				}
					
				if (thumbnail== null & actualItem.getURLOfAPictureInGroup()==null){
					thumbnail = defaultgroupThumbnail;
				}
				
				//width="120" height="80"
			%>
			<a href="<%= VIEW_VOCABULARY.RESTful_GROUP_URI_PATTERN +  actualResource.getId() %>"><img style="float:left; margin-right:1em; border: 0pt none" src="<%= thumbnail %>" width="120" height="80" alt="Group" onerror="this.src='<%=groupImageError %>';"/></a>
			<strong><a href="<%= VIEW_VOCABULARY.RESTful_GROUP_URI_PATTERN + actualResource.getId() %>"><%= actualResource.getName() %></a> (Group)</strong><br/>
			<% }else{
				%>
				<strong><a href="<%= VIEW_VOCABULARY.RESTful_RESOURCE_URI_PATTERN + actualResource.getId() %>"><%= actualResource.getName() %></a></strong><br/>
			<%
			}
			if (MODEL_CONSTANTS.RESOURCE_TYPE_IMAGE.equals(actualResource.getType())){%>
				<%= actualResource.getDescription() %><br/>
				<img  src="<%= actualResource.getURL()%>" width="120" height="80"/> 
				<% 
				}
			else if (MODEL_CONSTANTS.RESOURCE_TYPE_GROUP.equals(actualResource.getType())){%>
				
					<p> <span style="font-size:0.8em;color:gray">Created: TODO</span></p>
					<p> <span style="font-size:0.8em;color:black">
					<% if (actualResource.getDescription().length()>70) {%>
					<%= actualResource.getDescription().substring(0, 70)+"..."  %>
					<% }else { %>
					<%= actualResource.getDescription()%>
					<% }%>
					</span></p>
				<% 
			}
			else if (MODEL_CONSTANTS.RESOURCE_TYPE_SHOCKWAVE.equals(actualResource.getType())){%>
				<object>
				<param name="movie" value="<%= actualResource.getURL().toString() %>"></param>
				<param name="wmode" value="transparent"></param>
				<embed id="<%= actualResource.getName() %>_contentVideo" src="<%= actualResource.getURL().toString() %>"
						type="application/x-shockwave-flash" wmode="transparent">
				</embed>
				</object>
				<% 
			}
			else {%>
				<p> 
					<span style="font-size:0.8em;color:black">
					<% if(actualResource.getDescription().length() != 0){
						%>
						<%= actualResource.getDescription()%>
						<%
					}else {out.print("- no description - ");} %>
					</span>
				</p>
				<br/>
				<% 
			}
			if (it.hasNext()) { %> <br style="clear:left;"/>
			<% }; %>
			</div>
			
			<%
		};%>
		
</div>
